package com.lemeng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.lemeng.dao.BrandMapper;
import com.lemeng.dao.CategoryBrandMapper;
import com.lemeng.dao.TemplateMapper;
import com.lemeng.entity.PageResult;
import com.lemeng.pojo.Brand;
import com.lemeng.pojo.CategoryBrand;
import com.lemeng.service.CategoryBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service(interfaceClass = CategoryBrandService.class)
public class CategoryBrandServiceImpl implements CategoryBrandService {

    @Autowired
    CategoryBrandMapper categoryBrandMapper;

    @Override
    public List<CategoryBrand> findAll() {
        return categoryBrandMapper.selectAll();
    }

    @Override
    public PageResult<CategoryBrand> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        PageInfo<CategoryBrand> categoryBrandPageInfo = new PageInfo<>(categoryBrandMapper.selectAll());
//        Page<CategoryBrand> categoryBrands = (Page<CategoryBrand>)categoryBrandMapper.selectAll();
        return new PageResult(categoryBrandPageInfo.getTotal(),categoryBrandPageInfo.getList());
    }

    @Override
    public List<CategoryBrand> findList(Map<String, Object> serchMap) {
        Example example = new Example(CategoryBrand.class);
        Example.Criteria criteria = example.createCriteria();
        if (serchMap != null) {
            if (serchMap.get("name")!=null && StringUtil.isNotEmpty(serchMap.get("name").toString())){
                criteria.andLike("name","%"+serchMap.get("name").toString()+"%");
            }
            if (serchMap.get("templateId")!=null && StringUtil.isNotEmpty(serchMap.get("templateId").toString())){
                criteria.andEqualTo("templateId",serchMap.get("templateId").toString());
            }
        }
        return categoryBrandMapper.selectByExample(example);
    }

    @Override
    public PageResult<CategoryBrand> findPage(Integer page, Integer size, Map<String, Object> serchMap) {
        PageHelper.startPage(page,size);
        PageInfo<CategoryBrand> categoryBrandPageInfo = new PageInfo<>(findList(serchMap));
        return new PageResult(categoryBrandPageInfo.getTotal(),categoryBrandPageInfo.getList());
    }

    @Override
    public CategoryBrand findById(Integer id) {
        return categoryBrandMapper.selectByPrimaryKey(id);
    }

    @Autowired
    TemplateMapper templateMapper;

    @Override
    @Transactional
    public void add(CategoryBrand categoryBrand) {
        categoryBrandMapper.insert(categoryBrand);
    }

    @Override
    public void update(CategoryBrand categoryBrand) {
        categoryBrandMapper.updateByPrimaryKeySelective(categoryBrand);
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        categoryBrandMapper.deleteByPrimaryKey(id);
    }

    @Autowired
    BrandMapper brandMapper;

    @Override
    public List<Brand> findByCategoryIdTkMapper(Integer categoryId) {
        CategoryBrand categoryBrand = new CategoryBrand();
        categoryBrand.setCategoryId(categoryId);
        List<CategoryBrand> categoryBrandList = categoryBrandMapper.select(categoryBrand);
        if (!CollectionUtils.isEmpty(categoryBrandList)){
//            if (categoryBrandList!=null&&categoryBrandList.size()>0){
            List<Integer> brandIds = categoryBrandList.stream().map(ca -> ca.getBrandId()).collect(Collectors.toList());

            Example example = new Example(Brand.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andIn("id",brandIds);
            return brandMapper.selectByExample(example);
        }
        return new ArrayList<Brand>();

    }

    @Override
    public List<Brand> findBrandByCategoryIdByMybatisXml(Integer categoryId) {
        return categoryBrandMapper.findBrandByCategoryIdByMybatisXml(categoryId);
    }


}
